﻿using System;
using RayDen.Library.Core.Primitives;

namespace RayDen.Library.Data
{

    public class DefaultTextureEvaluator
    {
        
    }

    public class ProceduralTexture : BaseProceduralTexture
    {
        private string expression;
        private Func<object, RgbSpectrum> texFunc;

        public ProceduralTexture(int w, int h, string exp)
            : base(w, h)
        {
            this.expression = exp;

            if (texFunc == null)
            {
                var evaluator = new GenericExpressionEvaluator<RgbSpectrum>((i) =>
                {
                    if (i is float)
                    {
                        return new RgbSpectrum((float)i);
                    }
                    if (i is double)
                    {
                        return new RgbSpectrum((float)((double)i));
                    }
                    return (RgbSpectrum)i;
                },
                    RgbSpectrum.FromStringData);
                texFunc = evaluator.Compile(expression);
            }
        }

        public override RgbSpectrumInfo Sample(float u, float v)
        {
            var arg = new
            {
                x = u,
                y = v,
                unit = new RgbSpectrumInfo(1f),
                
            };

            return (RgbSpectrumInfo) texFunc(arg);
        }
    }
}